package com.example.tome.component_router.baseApp;

import android.content.Context;
import android.content.res.Configuration;


import com.example.tome.component_router.dagger.component.AppComponent;
import com.example.tome.component_router.dagger.component.DaggerAppComponent;
import com.example.tome.component_router.dagger.module.AppModule;
import com.example.tome.component_router.dagger.module.HttpModule;
import com.example.tome.component_widget_ui.helper.LocaleHelper;
import com.example.tome.component_widget_ui.utils.L;
import com.example.tome.module_base.base.CommonApplication;

import java.util.Locale;

/**
 * @Created by TOME .
 * @时间 2018/4/26 17:05
 * @描述 ${BaseApplication}
 */

public class BaseApplication extends CommonApplication {
    public static boolean DEBUG = true;//true 调试模式

    private static BaseApplication mInstance ;
    //dagger
    private static volatile AppComponent mAppComponent ;

    @Override
    protected void attachBaseContext(Context base) {
        CommonApplication.IS_DEBUG = DEBUG;
        super.attachBaseContext(base);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Locale _UserLocale = LocaleHelper.getLanguage(this);
        //系统语言改变了应用保持之前设置的语言
        if (_UserLocale != null) {
            LocaleHelper.setLocale(this, _UserLocale);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        L.i("是否为debug模式："+IS_DEBUG);
        mInstance = this ;

    }

    public static synchronized BaseApplication getInstance() {
        return mInstance;
    }

    public static synchronized AppComponent getAppComponent() {
        if (mAppComponent == null) {
            mAppComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(mInstance))
                    .httpModule(new HttpModule())
                    .build();
        }
        return mAppComponent;
    }
}
